<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  searchEngine
 */

class m_searchEngine_documents_Index
{
    private $id;
    private $websiteId;
    private $pageId;
    private $pageLang;
    private $description;
    private $tags;
    private $url;
    private $content;

    /**
     * @param integer $id
     * @return m_searchEngine_documents_Index
     */
    private function __construct($id = null)
    {
        if(!is_null($id))
        {
            $datas = f_database_Provider::getInstance()->getDatas('f_index', array('id' => $id));
            foreach($datas[0] as $k => $data)
            {
                $this->$k = $data;
            }
        }

        return $this;
    }

    /**
     * @return m_searchEngine_documents_Index
     */
    public static function createNew()
    {
        return new self();
    }

    /**
     * @return m_searchEngine_documents_Index
     */
    public static function createNewFromPage($page, $indexation = false)
    {
        $new = new self();

        $indexService = m_searchEngine_services_indexationService::getInstance($page);

        $content = $new->cleanContent($indexService->getPageContent($indexation));

        /* @var $page m_website_documents_page */
        $new    ->setWebsiteId($page->getWebsiteId())
                ->setPageId($page->getUniqueId())
                ->setPageLang($page->getLang())
                ->setContent($content)
                ->setDescription($indexService->getPageTitle())
                ->setTags('')
                ->setUrl($page->getUrl());

        return $new;
    }

    /**
     * @return m_searchEngine_documents_Index
     */
    public static function createNewFromDocument($page, $document)
    {
        $new = new self();

        $indexService = m_searchEngine_services_indexationService::getInstance($page);

        $content = method_exists($document, 'getContent') ? $document->getContent() : '';

        /* @var $page m_website_documents_page */
        $new    ->setWebsiteId($page->getWebsiteId())
                ->setPageId($page->getUniqueId())
                ->setPageLang($page->getLang())
                ->setContent($new->cleanContent($content))
                ->setDescription($document->getLabel())
                ->setTags('')
                ->setUrl($page->getUrl().'/'.$document->getUrl());

        return $new;
    }

    /**
     * @return m_searchEngine_documents_Index
     */
    public static function getInstanceByUniqueId($id)
    {
        return new self($id);
    }

    /**
     * @return m_searchEngine_documents_Index
     */
    public function setWebsiteId($value)
    {
        $this->websiteId = $value;
        return $this;
    }

    public function getWebsiteId()
    {
        return $this->websiteId;
    }

    /**
     * @return m_searchEngine_documents_Index
     */
    public function setPageId($value)
    {
        $this->pageId = $value;
        return $this;
    }

    public function getPageId()
    {
        return $this->getPageId();
    }

    /**
     * @return m_searchEngine_documents_Index
     */
    public function setPageLang($value)
    {
        $this->pageLang = $value;
        return $this;
    }

    public function getPageLang()
    {
        return $this->pageLang;
    }

    public function getDescription()
    {
        return $this->description;
    }

    /**
     * @return m_searchEngine_documents_Index
     */
    public function setDescription($value)
    {
        $this->description = $value;
        return $this;
    }

    /**
     * @return m_searchEngine_documents_Index
     */
    public function setTags($value)
    {
        $this->tags = $value;
        return $this;
    }

    public function getTags()
    {
        return $this->tags;
    }

    /**
     * @return m_searchEngine_documents_Index
     */
    public function setUrl($value)
    {
        $this->url = $value;
        return $this;
    }

    public function getUrl()
    {
        return $this->url;
    }

    public function getFullUrl()
    {
        $websiteUrl = f_document_Provider::getInstance()->getByModelAndUniqueId('website/Website', $this->getWebsiteId(), $this->getPageLang())->getUrl();

        return 'http://'.$websiteUrl.$this->getUrl();
    }

    /**
     * @return m_searchEngine_documents_Index
     */
    public function setContent($value)
    {
        $this->content = $value;
        return $this;
    }

    public function getContent()
    {
        return $this->content;
    }

    public function save()
    {
        $dbProvider = f_database_Provider::getInstance();

        if(!isset($this->id) || is_null($this->id))
        {
            if($this->checkProperties())
            {
                $dbProvider->insert('f_index', $this->getPropertiesArray());
            }
        }
    }

    private function checkProperties()
    {
        $properties = $this->getPropertiesArray();

        foreach($properties as $k => $property)
        {
            if($k != 'id')
            {
                if(is_null($property)) return false;
            }
        }

        if($this->content == '') return false;

        return true;
    }

    private function getPropertiesArray()
    {
        $properties = (array)$this;
        
        foreach($properties as $key => $value)
		{
			$keys = explode("\0", $key);
			$fieldName = end($keys);
            $content[$fieldName] = $value;
        }

        return $content;
    }

    private function cleanContent($content)
    {
        $content = strip_tags($content);
        $content = htmlspecialchars_decode($content);
        $content = html_entity_decode($content, ENT_COMPAT, "UTF-8");
        $content = preg_replace('/\s\s+/', ' ', $content);
        $content = preg_replace('/\n+/', ' ', $content);

        return trim($content);
    }
}